﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.Syndication;
using System.Xml;


namespace DragDraw.Tool
{
    public partial class RSSReader : UserControl
    {
        int m_index = -1;
        List<SyndicationItem> m_feeds = new List<SyndicationItem>();
        public RSSReader()
        {
            InitializeComponent();
        }

        public void ReadRss(string url)
        {
            WebRequest request = WebRequest.Create(new System.Uri(url));
            request.BeginGetResponse(new AsyncCallback(OnRssResponse), request);
        }

        void OnRssResponse(IAsyncResult asyncResult)
        {
            WebRequest request = (WebRequest)asyncResult.AsyncState;
            WebResponse response = (WebResponse)request.EndGetResponse(asyncResult);

            XmlReader reader = XmlReader.Create(response.GetResponseStream());
            SyndicationFeed feed = SyndicationFeed.Load(reader);

            m_feeds = feed.Items.ToList<SyndicationItem>();
            m_index = 0;
        }

        private void BtnPre_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (m_feeds.Count <= 0) return;
            m_index = (m_index + m_feeds.Count - 1) % m_feeds.Count;
            TxtRss.Text = m_feeds[m_index].ToString();
        }

        private void BtnNext_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (m_feeds.Count <= 0) return;
            m_index = (m_index + m_feeds.Count + 1) % m_feeds.Count;
            TxtRss.Text = m_feeds[m_index].ToString();
        }
    }
}
